﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using MyCommerce.Presentation.Stores.IViews;
using MyCommerce.Presentation.Stores.Presenters;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class ProductFillView : BaseForm, IProductFill
    {
        private ProductFillPresenter pPresenter;

        public ProductFillView()
        {
            InitializeComponent();
            pPresenter = new ProductFillPresenter(this);
        }

        #region IEmployeeFillView Members

        public long IdItemEdit { get; set; }

        public OptionSelect OptionViewSelect { get; set; }

        public List<Classifier> ColCategory
        {
            set
            {
                cmbCategory.DisplayMember = "Name";
                cmbCategory.ValueMember = "IdClassifier";
                cmbCategory.DataSource = value;
            }
        }

        public List<Classifier> ColMark
        {
            set
            {
                cmbMark.DisplayMember = "Name";
                cmbMark.ValueMember = "IdClassifier";
                cmbMark.DataSource = value;
            }
        }

        public List<Classifier> ColProductType
        {
            set
            {
                cmbProductType.DisplayMember = "Name";
                cmbProductType.ValueMember = "IdClassifier";
                cmbProductType.DataSource = value;
            }
        }

        public Product Get()
        {
            Product objProduct = new Product();
            objProduct.IdcCategory =(long)cmbCategory.SelectedValue;
            objProduct.IdcMark = (long)cmbMark.SelectedValue;
            objProduct.IdcProductType= (long)cmbProductType.SelectedValue;
            objProduct.Model = txtModel.Text.Trim();
            objProduct.Description=txtDescription.Text.Trim();
            if (checkColor.Checked)
            {
                objProduct.Color = colorProduct.Color.ToArgb();
            }
            else
            {
                objProduct.Color = 0;
            }            
            objProduct.WithGuarantee = checkWithGuarantee.Checked;
            objProduct.ProductLink = txtProductLink.Text;
            objProduct.OtherDescription = txtOther.Text.Trim();
            return objProduct;
        }

        public void Set(Product objProductToLoad)
        {
            IdItemEdit = objProductToLoad.IdProduct;
            if (String.IsNullOrEmpty(cmbCategory.ValueMember) == false
                && objProductToLoad.IdcCategory > 0)
                cmbCategory.SelectedValue = objProductToLoad.IdcCategory;
            if (String.IsNullOrEmpty(cmbMark.ValueMember) == false
                && objProductToLoad.IdcMark > 0)
                cmbMark.SelectedValue = objProductToLoad.IdcMark;
            if (String.IsNullOrEmpty(cmbProductType.ValueMember) == false
                && objProductToLoad.IdcProductType > 0)
                cmbProductType.SelectedValue = objProductToLoad.IdcProductType;
            txtModel.Text = objProductToLoad.Model;
            txtDescription.Text = objProductToLoad.Description;
            if (objProductToLoad.Color == 0)
            {
                checkColor.Checked = false;
                colorProduct.Enabled = false;
            }
            else
            {
                checkColor.Checked = true;
                colorProduct.Color = Color.FromArgb(Convert.ToInt32(objProductToLoad.Color));
            }
            checkWithGuarantee.Checked = objProductToLoad.WithGuarantee;
            txtProductLink.Text = objProductToLoad.ProductLink;
            txtOther.Text = objProductToLoad.OtherDescription;            
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de productos.");
            Result = result;
            Close();
        }

        public event GenericEventHandler<Product> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(txtModel.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar un modelo del producto seleccionado.");
            }
            
            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void EmployeeFillView_Load(object sender, EventArgs e)
        {
            Product objProduct = null;
            OptionViewSelect = OptionSelect.Create;
            if (Parameter != null)
            {
                objProduct = (Product)Parameter;
                OptionViewSelect = OptionSelect.Edit;

                if (objProduct.Color == 0)
                {
                    checkColor.Checked = false;
                    colorProduct.Enabled = false;
                }
            }
            else
            {
                checkColor.Checked = false;
                colorProduct.Enabled = false;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<Product>(objProduct));
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                        OnSave(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnChargeCategory(sender,  new GenericEventArgs<long>(Convert.ToInt64(cmbCategory.SelectedValue)));
        }

        private void ultraCheckEditor1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkColor.Checked)
            {
                colorProduct.Enabled = true;
            }
            else
            {
                colorProduct.Enabled = false;
            }
        }


        #region IProductFill Members


        public event GenericEventHandler<long> OnChargeCategory;

        #endregion
    }
}
